<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_Promise初体验2</title>
</head>

<body>
<button>抽奖</button>
<script>

  function rand(m,n){
    return Math.ceil(Math.random()*(n-m+1))+m-1
  }

  const btn=document.querySelector('button')
  btn.addEventListener('click',function () {
    const p=new Promise((resolve,reject)=>{
      setTimeout(()=>{

        let n=rand(1,100)
        if (n<=30){
          resolve(n)  //成功时传入的值
        }
        else reject(n)
      },1000)
    })

    p.then((value)=>{ //接受并处理成功时传入的值
              alert('恭喜你中奖了'+value)

            },
            (reason)=>{
              alert('很遗憾你没有中奖'+reason)
            }
    )
  })

</script>
</body>
</html>